package api

import (
	"api-getaway/basic/config"
	"api-getaway/basic/model"
	"api-getaway/handler/request"
	"encoding/json"
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
	"time"
)

func VideoAdd(r *gin.Context) {
	var req request.Video
	err := r.ShouldBind(&req)
	if err != nil {
		r.JSON(http.StatusBadRequest, gin.H{
			"code":    400,
			"message": "参数错误",
			"data":    nil,
		})
		return
	}
	video := model.Video{
		Title:        req.Title,
		Description:  req.Description,
		CoverUrl:     req.CoverUrl,
		CategoryId:   req.CategoryId,
		TypeId:       req.TypeId,
		RegionId:     req.RegionId,
		Year:         req.Year,
		Actor:        req.Actor,
		Director:     req.Director,
		PlayCount:    req.PlayCount,
		LikeCount:    req.LikeCount,
		CollectCount: req.CollectCount,
		CommentCount: req.CommentCount,
		Status:       req.Status,
	}
	if err := video.CreateVideo(); err != nil {
		r.JSON(http.StatusBadRequest, gin.H{
			"code":    400,
			"message": "视频创建失败",
			"data":    nil,
		})
		return
	}

	r.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "视频信息添加成功",
		"data":    video,
	})
}

func VideoReviewAdd(r *gin.Context) {
	var req request.VideoReview
	err := r.ShouldBind(&req)
	if err != nil {
		r.JSON(http.StatusBadRequest, gin.H{
			"code":    400,
			"message": "参数错误",
			"data":    nil,
		})
		return
	}

	review := model.VideoReview{
		VideoId:  req.VideoId,
		Status:   req.Status,
		Reason:   req.Reason,
		ReviewId: req.ReviewId,
	}

	if err := review.CreateVideoReview(); err != nil {
		r.JSON(http.StatusBadRequest, gin.H{
			"code":    400,
			"message": "视频审核失败",
			"data":    nil,
		})
		return
	}

	r.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "视频审核成功",
		"data":    review,
	})
}

func VideoList(r *gin.Context) {
	var req request.Fen
	err := r.ShouldBind(&req)
	if err != nil {
		r.JSON(http.StatusBadRequest, gin.H{
			"code":    400,
			"message": "参数错误",
			"data":    nil,
		})
		return
	}

	page := req.Page
	if page <= 0 {
		page = 1
	}

	pageSize := req.Size
	switch {
	case pageSize > 100:
		pageSize = 100
	case pageSize <= 0:
		pageSize = 10
	}

	offset := (page - 1) * pageSize

	var all []model.Video

	config.DB.Debug().Offset(offset).Limit(pageSize).Find(&all)
	r.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "视频列表展示成功",
		"data":    all,
	})
}

func VideoFilter(r *gin.Context) {
	var req request.Video
	err := r.ShouldBind(&req)
	if err != nil {
		r.JSON(http.StatusBadRequest, gin.H{
			"code":    400,
			"message": "参数错误",
			"data":    nil,
		})
		return
	}

	var video []model.Video

	key := fmt.Sprintf("video_filter_")
	result := config.Rdb.Get(config.Ctx, key)
	if result.Val() != "" {
		fmt.Println("使用缓存")
		json.Unmarshal([]byte(key), &video)
	} else {
		marshal, _ := json.Marshal(video)
		config.DB.Debug().Find(&video)
		config.Rdb.Set(config.Ctx, key, marshal, time.Minute*5)
	}

	r.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "视频筛选成功",
		"data":    video,
	})
}

func VideoShow(r *gin.Context) {
	var req request.Video
	err := r.ShouldBind(&req)
	if err != nil {
		r.JSON(http.StatusBadRequest, gin.H{
			"code":    400,
			"message": "参数错误",
			"data":    nil,
		})
		return
	}

	var all model.Video
	config.DB.Debug().Find(&all)
	r.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "视频详情展示成功",
		"data":    all,
	})
}

func EpisodeList(r *gin.Context) {
	var req request.DramaEpisode
	err := r.ShouldBind(&req)
	if err != nil {
		r.JSON(http.StatusBadRequest, gin.H{
			"code":    400,
			"message": "参数错误",
			"data":    nil,
		})
		return
	}

	var all []model.DramaEpisode
	config.DB.Debug().Find(&all)
	r.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "剧集列表展示成功",
		"data":    all,
	})
}

func HistoryStorage(r *gin.Context) {
	var req request.UserHistory
	err := r.ShouldBind(&req)
	if err != nil {
		r.JSON(http.StatusBadRequest, gin.H{
			"code":    400,
			"message": "参数错误",
			"data":    nil,
		})
		return
	}

	history := model.UserHistory{
		UserId:    req.UserId,
		VideoId:   req.VideoId,
		EpisodeId: req.EpisodeId,
		Progress:  req.Progress,
	}

	if err := history.CreateHistory(); err != nil {
		r.JSON(http.StatusBadRequest, gin.H{
			"code":    400,
			"message": "历史记录存储失败",
			"data":    nil,
		})
		return
	}

	r.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "历史记录存储成功",
		"data":    history,
	})
}

func HistoryList(r *gin.Context) {
	var req request.UserHistory
	err := r.ShouldBind(&req)
	if err != nil {
		r.JSON(http.StatusBadRequest, gin.H{
			"code":    400,
			"message": "参数错误",
			"data":    nil,
		})
		return
	}

	var all []model.UserHistory
	config.DB.Debug().Find(&all)
	r.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "历史记录查询成功",
		"data":    all,
	})
}

func RankingList(r *gin.Context) {
	var req request.Ranking
	err := r.ShouldBind(&req)
	if err != nil {
		r.JSON(http.StatusBadRequest, gin.H{
			"code":    400,
			"message": "参数错误",
			"data":    nil,
		})
		return
	}

	var all []model.Ranking
	config.DB.Debug().Find(&all)
	r.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "排行榜查询成功",
		"data":    all,
	})
}
